﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.IDAL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class HrmScopeDAL : IDALHrmScope
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public HrmScopeDAL()
        {

        }

        public IList<SysScopeInfo> GetHrmScope(string record_status, int warehouse_seq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<SysScopeInfo> infoList = new List<SysScopeInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@record_status", record_status);
                dbManager.AddParameters(1, "@warehouse_seq", warehouse_seq);

                strSQL = "SELECT DISTINCT wa.SCOPE_SEQ"
                        //+ ", CASE WHEN SCOPE = 1 THEN 'หน่วยงานส่วนกลาง'"
                        //    + " WHEN SCOPE = 2 THEN 'หน่วยงานส่วนภูมิภาค'"
                        //    + " WHEN SCOPE = 3 THEN 'หน่วยงานต่างประเทศ'"
                        //+ " END AS SCOPE_NAME"
                        + ", ms.SCOPE_NAME"
                        + ", (select count(wc.job_site_seq) from icm_mas_warehouse_consume wc"
                            + " inner join hrm_mp_job_sites js on wc.job_site_seq = js.job_site_seq"
                            + " left join hrm_mp_work_areas wa on js.work_area_seq = wa.work_area_seq"
                            + " where wc.icm_wh_seq = @warehouse_seq"
                                + " and wa.scope_seq = war.SCOPE_SEQ"
                        + " ) AS JOBSITE_COUNT"
                        + " FROM HRM_MP_WORK_AREAS war"
                            + " LEFT JOIN HRM_MP_SCOPES ms ON wa.SCOPE_SEQ = ms.SCOPE_SEQ"
                        + " WHERE war.RECORD_STATUS = @record_status";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        SysScopeInfo info = new SysScopeInfo();
                        info.Name = dr[i]["SCOPE_NAME"].ToString();
                        info.Seq = Convert.ToInt32(dr[i]["SCOPE_SEQ"]);
                        info.Count = Convert.ToInt32(dr[i]["JOBSITE_COUNT"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
    }
}
